#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dos.h>
#include <conio.h>
#define LENGTH sizeof(struct ContactPerson)
struct ContactPerson
{
    char Name[20];
	char Occupation[20];
	char Title[20];
	char Phonenumber[20];
	char Email[20];
};
//构建联系人信息结构体

struct ContactPerson Infor[100];//接收数据的地址

void Display()
{
	FILE *fp;
	int i;
	int count = 0;
	fp = fopen("data.txt","rb");
	while(!feof(fp))
	{
		  
		if(fread(&Infor[count], LENGTH, 1, fp) ==1)
		    count++	;		
    }
    fclose(fp);
    printf("%-20s%-20s%-20s%-20s%-20s\n","姓名","职务","职称","电话","邮箱");
    for(i=0; i<count; i++)
	{
    	 printf("%-20s%-20s%-20s%-20s%-20s\n",Infor[i].Name,Infor[i].Occupation,Infor[i].Title,Infor[i].Phonenumber,Infor[i].Email);
	}
}

void Input()
{
	int i;
	int count = 100;
	char ch[2];//
	FILE *fp;
	if((fp=fopen("data.txt","a+"))==NULL)//创建文件储存信息
	{
		printf("无法打开\n");
		return;
	}
	while(!feof(fp))//判断文件是否结束
	{
		if(fread(&Infor[100], LENGTH, 1, fp)==1)
		count++;
	}
	fclose(fp);//关闭文件
	if(count == 0)//查看当前通讯录是否为空
	{
		printf("通讯录为空\n");
	}
	else
	{
		system("cls");//清屏
		Display();
	}
	if((fp=fopen("data.txt","wb"))==NULL)//如果无法打开文件
	{
		printf("不能打开\n");
		return;
	}
	for(i=0; i<count; i++)//重新录入数据
	{
		fwrite(&Infor[i], LENGTH, 1, fp);
		printf("please input(yes or no)\n");
		scanf("%s",ch);
		while(strcmp(ch, "yes") == 0)
		{
			printf("请输入姓名：\n");
		    scanf("%s",&Infor[count].Name);
		    printf("请输入职位：\n");
		    scanf("%s",&Infor[count].Occupation);
		    printf("请输入职称：\n");
		    scanf("%s",&Infor[count].Title);
		    printf("请输入电话：\n");
		    scanf("%s",&Infor[count].Phonenumber);
		    printf("请输入邮箱：\n");
		    scanf("%s",&Infor[count].Email);
		    
		    if(fwrite(&Infor[count], LENGTH, 1, fp) != 1)
		    {
		    	printf("无法录入\n");
			}
			else
			{
				printf("已录入\n");
				count++;
			}
			fclose(fp);
		}
    }
}
void Delete()
{
	FILE *fp;
	int i;
	int j;
	int count = 0;
	char ch[2];
	char Name[20];
	if((fp=fopen("data.txt","r+"))==NULL)//创建文件储存信息
	{
		printf("无法打开\n");
		return;
	}
	while(!feof(fp))//判断文件是否结束
	{
		if(fread(&Infor[100], LENGTH, 1, fp)==1)
		count++;
	}
	fclose(fp);//关闭文件
	if(count == 0)//查看当前通讯录是否为空
	{
		printf("通讯录为空\n");
	}
	else
	{
		system("cls");//清屏
		Display();
	}
	printf("请输入要删除联系人的姓名\n");
	scanf("%s",&Name);
	for(i=0; i<count; i++)
	{
		if(strcmp(Name, Infor[i].Name) == 0)
		{
			printf("确定要删除此联系人吗？(yes or no)\n");
			scanf("%s", ch);
			if(strcmp(ch, "yes") == 0)
				for(j=i; j < count; j++)
			        Infor[j] = Infor[j+1];
			count--;
			if((fp=fopen("data.txt","wb"))==NULL)//如果无法打开文件
	        {
		        printf("不能打开\n");
		        return;
	        }
	        for(j=0; j<count; j++)
	            if(fwrite(&Infor[j], LENGTH, 1, fp) != 1)
		    {
		    	printf("无法录入\n");
			}
			fclose(fp);
			printf("已成功删除\n");
			return;
		}
	}
}
		
void Update()
{
	FILE *fp;
	int i;
	int j;
	int count = 0;
	char ch[2];
	char Name[20];
	if((fp=fopen("data.txt","r+"))==NULL)
	{
		printf("无法打开\n");
		return;
	}
	while(!feof(fp))
	{
		  
		if(fread(&Infor[count], LENGTH, 1, fp) ==1)
		    count++	;		
    }
	if(count == 0)
	{
		printf("通讯录为空\n");
		fclose(fp);
		return;
	}	
	printf("请输入要更新信息联系人的姓名:\n");
	scanf("%s",&Name);
	for(i=0; i<count; i++)
	{
		if(strcmp(Name, Infor[i].Name) == 0)
		{
			printf("请输入姓名：\n");
		    scanf("%s",&Infor[count].Name);
		    printf("请输入职位：\n");
		    scanf("%s",&Infor[count].Occupation);
		    printf("请输入职称：\n");
		    scanf("%s",&Infor[count].Title);
		    printf("请输入电话：\n");
		    scanf("%s",&Infor[count].Phonenumber);
		    printf("请输入邮箱：\n");
		    scanf("%s",&Infor[count].Email);
		    printf("已更新成功\n");
		    if((fp=fopen("data.txt","wb"))==NULL)//如果无法打开文件
	        {
		        printf("不能打开\n");
		        return;
	        }
	        for(j=0; j<count; j++)
	            if(fwrite(&Infor[j], LENGTH, 1, fp) != 1)
		    {
		    	printf("无法录入\n");
			}
			fclose(fp);
			return;
		}
	}
}
	

	
void Search()
{
	FILE *fp;
	int i;
	int count = 0;
	char Name[20];
	char ch;
	if((fp=fopen("data.txt","r+"))==NULL)
	{
		printf("无法打开\n");
		return;
	}
	while(!feof(fp))
	{
		  
		if(fread(&Infor[count], LENGTH, 1, fp) ==1)
		    count++	;		
    }
    fclose(fp);
    if(count == 0)
	{
		printf("通讯录为空\n");
		return;
	}	
	printf("请输入要查找人的姓名:\n");
	scanf("%s",&Name);
	for(i=0; i<count; i++)
	{
		if(strcmp(Name, Infor[i].Name) == 0)
		{
			printf("%-20s%-20s%-20s%-20s%-20s\n","姓名","职务","职称","电话","邮箱");
	        printf("%-20s%-20s%-20s%-20s%-20s\n",Infor[i].Name,Infor[i].Occupation,Infor[i].Title,Infor[i].Phonenumber,Infor[i].Email);
		}
    }
}

void Sort()
{
    FILE *fp;
	struct ContactPerson temp;
	int i = 0;
	int j = 0;
	int count = 0;
	if((fp=fopen("data.txt","r+"))==NULL)
	{
		printf("无法打开\n");
		return;
	}
	while(!feof(fp))
	{
		if(fread(&Infor[count], LENGTH, 1, fp) ==1)
		    count++	;		
    }
    fclose(fp);
    if(count == 0)
	{
		printf("通讯录为空\n");
		return;
	}
	for(i=0; i<count-1; i++)
	{
		for(j=i+1; j<count; j++)
		{
			if(strcmp(Infor[i].Name, Infor[j].Name) > 0)
			{
				temp = Infor[i];
				Infor[i] = Infor[j];
				Infor[j] = temp;
			}
		}
	}
	if((fp=fopen("data.txt","wb")) == NULL)//如果无法打开文件
	    {
		    printf("不能打开\n");
		    return;
	    }
	    for(i=0; i<count; i++)
	    {
		
	        if(fwrite(&Infor[i], LENGTH, 1, fp) != 1)
		    {
		    	printf("无法录入\n");
			}
	    }
		fclose(fp);
}


int main()
{
	int n;
	
	printf("----------菜单----------\n");
	printf("-    1.添加联系人      -\n");
	printf("-    2.删除联系人      -\n");
	printf("-    3.修改联系人      -\n");
	printf("-    4.显示联系人      -\n");
	printf("-    5.查找联系人      -\n");
	printf("-    6.排序并输出      -\n");
	printf("------------------------\n");
	printf("请输入：\n");
	scanf("%s",&n);
	
	switch(n)
	{
		case '1':
			Input();
			break;
		case '2':
			Delete();
			break;
		case '3':
			Update();
			break;
		case '4':
			Display();
			break;
		case '5':
			Search();
			break;
		case '6':
		    Sort();
			break;
		default:
			printf("输入错误");
			break;
	}
}

